const {
  getBlogList,
  getDetail,
  newBlog,
  updateBlog,
  delBlog,
} = require('../controller/blog')
const { SuccessModel, ErrorModel } = require('../module/resModel') // 定义返回响应的格式model
const blogRouter = (req, res) => {
  const path = req.path
  const method = req.method
  if (path === '/api/blog/list' && method === 'get') {
    const { author, key } = req.query
    let data = getBlogList(author, key)
    return new SuccessModel(data)
  } else if (path === '/api/blog/detail' && method === 'get') {
    const { id } = req.query
    if (id) return new SuccessModel(getDetail(id))
    else return new ErrorModel('id为空')
  } else if (path === '/api/blog/new' && method === 'post') {
    const blogData = req.body
    if (blogData) {
      return new SuccessModel(newBlog(blogData))
    } else return new ErrorModel('内容为空')
  } else if (path === '/api/blog/update' && method === 'post') {
    const { id } = req.query
    const blogData = req.body
    if (id) {
      return new SuccessModel(updateBlog(id, blogData))
    } else return new ErrorModel('id为空')
  } else if (path === '/api/blog/delete' && method === 'post') {
    const { id } = req.query
    if (id) return new SuccessModel(delBlog(id))
    else return new ErrorModel('id为空')
  } else return false
}
module.exports = blogRouter
